@using OrchardCore.Users.Models;
@model RegistrationSettings

<script asp-name="codemirror" depends-on="admin" at="Foot"></script>
<script asp-name="codemirror-mode-javascript" at="Foot"></script>
<script asp-name="codemirror-addon-display-autorefresh" at="Foot"></script>
<script asp-name="codemirror-addon-mode-simple" at="Foot"></script>
<script asp-name="codemirror-addon-mode-multiplex" at="Foot"></script>
<script asp-name="codemirror-mode-xml" at="Foot"></script>

<div class="form-group" asp-validation-class-for="UsersCanRegister">
    <label asp-for="UsersCanRegister">@T["Configure users registration."]</label>
    <select asp-for="UsersCanRegister" class="form-control">
        @foreach (UserRegistrationType option in Enum.GetValues(typeof(UserRegistrationType)))
        {
            <option value="@option" selected="@(Model.UsersCanRegister == option)">@T[option.ToString()]</option>
        }
    </select>
    <span asp-validation-for="UsersCanRegister"></span>

    <div class="hint">
        <ul>
            <li>@T["when NoRegistration is selected, no new users can register"]</li>
            <li>@T["when AllowRegistration is selected, a link is displayed to allow the users to register on the site. If there are external providers, once authenticated the user can register"]</li>
            <li>@T["when AllowOnlyExternalUsers is selected, only users authenticated with external providers can register on the site"]</li>
        </ul>
    </div>
</div>

<div class="form-group" asp-validation-class-for="UsersMustValidateEmail">
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" asp-for="UsersMustValidateEmail" data-toggle="collapse" data-target="#warnEmailValidation" />
        <span asp-validation-for="UsersMustValidateEmail"></span>
        <label class="custom-control-label" asp-for="UsersMustValidateEmail">@T["Users must verify their email address"]</label>
    </div>
</div>

<div class="alert alert-warning collapse" id="warnEmailValidation">@T["The users whose email address has not been confirmed will not be able to login or reset their password."]</div>

<div class="form-group" asp-validation-class-for="UseSiteTheme">
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" asp-for="UseSiteTheme" />
        <span asp-validation-for="UseSiteTheme"></span>
        <label class="custom-control-label" asp-for="UseSiteTheme">@T["Use site theme for registration page"]</label>
        <span class="hint">— @T["Requires an active site theme."]</span>
    </div>
</div>

<h5 class="form-group">@T["External Authentication"]<span class="hint"> — @T["Settings when registering with external authentication providers"]</span></h5>

<div class="form-group" asp-validation-class-for="UseScriptToGenerateUsername">
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" asp-for="UseScriptToGenerateUsername" />
        <span asp-validation-for="UseScriptToGenerateUsername"></span>
        <label class="custom-control-label" asp-for="UseScriptToGenerateUsername">@T["Use a script to generate userName based on external provider claims"]</label>
        <span class="hint">— @T["If selected, any IExternalLoginEventHandlers defined in modules are not triggered"]</span>
    </div>
    <pre>
********************************************************************************************
* context          : {userName,loginProvider,externalClaims[]}                             *
* ======================================================================================== *
* -userName        : String                                                                *
* -loginProvider   : String                                                                *
* -externalClaims  : [{subject,issuer,originalIssuer,properties[],type,value,valueType}]   *
*  -subject        : String                                                                *
*  -issuer         : String                                                                *
*  -originalIssuer : String                                                                *
*  -properties     : [{key,value}]                                                         *
*   -key           : String                                                                *
*   -value         : String                                                                *
*  -type           : String                                                                *
*  -value          : String                                                                *
*  -valueType      : String                                                                *
* ======================================================================================== *
*    Description                                                                           *
* ---------------------------------------------------------------------------------------- *
*    Use the loginProvider and externalClaims properties of the context variable to        *
*    inspect who authenticated the user and with what claims. Set the userName property    *
*    to the desired value                                                                  *
********************************************************************************************
</pre>
</div>
<div class="form-group" asp-validation-class-for="GenerateUsernameScript">
    <button type="button" class="btn btn-secondary mb-2" onclick="resetScript()">@T["Reset Script"]</button>
    <textarea asp-for="GenerateUsernameScript" rows="1" class="form-control content-preview-text"></textarea>
</div>

<div class="form-group" asp-validation-class-for="NoUsernameForExternalUsers">
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" asp-for="NoUsernameForExternalUsers" data-external-registration />
        <span asp-validation-for="NoUsernameForExternalUsers"></span>
        <label class="custom-control-label" asp-for="NoUsernameForExternalUsers">@T["Do not ask username"]</label>
        <span class="hint">— @T["When a new user logs in with an external provider, he is not required to provide a local username.You can customize how it looks by providing an IExternalLoginEventHandler or write a script."]</span>
    </div>
</div>
<div class="form-group" asp-validation-class-for="NoEmailForExternalUsers">
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" asp-for="NoEmailForExternalUsers" data-external-registration />
        <span asp-validation-for="NoEmailForExternalUsers"></span>
        <label class="custom-control-label" asp-for="NoEmailForExternalUsers">@T["Do not ask email address"]</label>
        <span class="hint">— @T["When a new user logs in with an external provider and the email claim is defined, he is not required to provide a local email address."]</span>
    </div>
</div>
<div class="form-group" asp-validation-class-for="NoPasswordForExternalUsers">
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" asp-for="NoPasswordForExternalUsers" data-external-registration />
        <span asp-validation-for="NoPasswordForExternalUsers"></span>
        <label class="custom-control-label" asp-for="NoPasswordForExternalUsers">@T["Do not create local password for external users"]</label>
        <span class="hint">— @T["When a new user logs in with an external provider, he is not required to provide a local password."]</span>
    </div>
</div>

<script at="Foot">
    //<![CDATA[
    function refresh(e) {
        var usersCanRegisterElement = document.getElementById("@Html.IdFor(m => m.UsersCanRegister)");
        var externalRegistrationInputs = $("input[data-external-registration]");

        if (usersCanRegisterElement.value == "NoRegistration") {
            externalRegistrationInputs.prop('disabled', true);
        } else {
            externalRegistrationInputs.prop('disabled', false);
        }
    }

    function resetScript(keepText) {
        var editor = $('#@Html.IdFor(x => x.GenerateUsernameScript)').data('editor');
        if (!keepText) {
            editor.doc.setValue(
                '/* Uncomment to map AzureAD\n' +
                'switch (context.loginProvider) {\n' +
                '    case "AzureAD":\n' +
                '        context.userName = "azad" + Date.now().toString();\n' +
                '        break;\n' +
                '    default:\n' +
                '        log("Warning", "Provider " + loginProvider + " was not handled");\n' +
                '        break;\n' +
                '}\n' +
                '*/\n'
            );
        }
    }

    window.onload=function () {
        refresh();

        $("#@Html.IdFor(m => m.UsersCanRegister)").change(function () {
            refresh();
        });

        var textArea = document.getElementById('@Html.IdFor(x => x.GenerateUsernameScript)');
        if (textArea == null) {
            return;
        }
        var editor = CodeMirror.fromTextArea(textArea, {
            autoRefresh: true,
            lineNumbers: true,
            styleActiveLine: true,
            matchBrackets: true,
            autoCloseTags: true,
            mode: "javascript"
        });
        $('#@Html.IdFor(x => x.GenerateUsernameScript)').data('editor', editor);
        resetScript(editor.doc.lineCount() > 1);
    };
    //]]>
</script>
